//
//  RiskViewer.m
//  nova
//
//  Created by OSX on 8/17/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "RiskViewer.h"
#import "Risk.h"


@implementation RiskViewer

@synthesize risk;
@synthesize dateFormatter;
@synthesize abbreviation1;
@synthesize abbreviation2;
@synthesize highlighted;
@synthesize editing;

- (id) initWithFrame:(CGRect)frame {
    
    if (self == [super initWithFrame:frame]) {  
        
        dateFormatter = [[NSDateFormatter alloc] init];        
        [dateFormatter setDateFormat:@"yyyy-MM-dd"];        
        self.opaque = YES;        
        //self.backgroundColor = [UIColor whiteColor];
        self.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"background.png"]];
    }    
    return self;    
}

- (void)setRisk:(Risk *)newRisk {   
    
    if (risk != newRisk) {        
        [risk release];        
        risk = [newRisk retain]; 
        
        NSString *string = [[NSString alloc] initWithFormat:@"Cause: %@", [risk getCause]];
        NSString *string2 = [[NSString alloc] initWithFormat:@"Effect: %@", [risk getEffect]];
        
        self.abbreviation1 = string;
        self.abbreviation2 = string2;
        
        [string release];
        
    }    
    [self setNeedsDisplay];
    
}

- (void)setHighlighted:(BOOL)lit {
    
    // If highlighted state changes, need to redisplay.
    
    if (highlighted != lit) {        
        highlighted = lit;          
        [self setNeedsDisplay];        
    }    
}


- (void)drawRect:(CGRect)rect {   
    
    
#define LEFT_COLUMN_OFFSET 10
#define LEFT_COLUMN_WIDTH 150
#define TOTAL_WIDTH 320
    
#define MIDDLE_COLUMN_OFFSET 170    
#define MIDDLE_COLUMN_WIDTH 110    
    
#define RIGHT_COLUMN_OFFSET 270   
    
#define UPPER_ROW_TOP 5   
#define LOWER_ROW_TOP 60    
#define LOWER_ROW_TOP2 75
    
#define FONT_SIZE 13
#define MIN_FONT_SIZE 12    
//#define MIN_MAIN_FONT_SIZE 14    
//#define SECONDARY_FONT_SIZE 12    
//#define MIN_SECONDARY_FONT_SIZE 10
#define RGB(r, g, b) [UIColor colorWithRed:r/255.0 green:g/255.0 blue:b/255.0 alpha:1]
    
    UIColor *mainTextColor = nil;    
    UIFont *mainFont = [UIFont systemFontOfSize:FONT_SIZE];
    
    
    UIColor *secondaryTextColor = nil;    
    UIFont *secondaryFont = [UIFont systemFontOfSize:MIN_FONT_SIZE];
    
    
    if (self.highlighted) {        
        mainTextColor = [UIColor whiteColor];        
        secondaryTextColor = [UIColor whiteColor];        
    }    
    else {        
        mainTextColor = RGB(102,102,102);        
        secondaryTextColor = RGB(102,102,102);;        
        self.backgroundColor = [UIColor colorWithPatternImage: [UIImage imageNamed: @"background.png"]];
    }
    
    CGRect contentRect = self.bounds;   
    
    if (!self.editing) {
        
        CGFloat boundsX = contentRect.origin.x;        
        CGPoint point;                     
        CGFloat actualFontSize;        
        CGSize size;
        
        // Set the color for the main text items.
        
        [mainTextColor set];   
        
        
        point = CGPointMake(boundsX + LEFT_COLUMN_OFFSET, UPPER_ROW_TOP);
        
        NSString *riskLabel = [NSString stringWithFormat:@"Risk: #%d", [risk getID]];
        
        [riskLabel drawAtPoint:point forWidth:LEFT_COLUMN_WIDTH withFont:mainFont minFontSize:FONT_SIZE actualFontSize:NULL lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
        
        // Monitor Finish ***********
        
        NSDate *monitorFinDate  = [NSDate dateWithTimeIntervalSince1970:[risk getMonitorFinish]];
        
        NSString *monitorFinish = [NSString stringWithFormat:@"Monitoring: %@", [dateFormatter stringFromDate: monitorFinDate]];
        
        size = [monitorFinish sizeWithFont:mainFont minFontSize:FONT_SIZE actualFontSize:&actualFontSize forWidth:TOTAL_WIDTH lineBreakMode:UILineBreakModeTailTruncation];  
        
        CGFloat finish = (TOTAL_WIDTH - size.width) - 10;
        
        point = CGPointMake(finish, UPPER_ROW_TOP);
        
        //int width = TOTAL_WIDTH - MIDDLE_COLUMN_OFFSET;
        int width = size.width;
        
        [monitorFinish drawAtPoint:point forWidth:width withFont:mainFont minFontSize:FONT_SIZE actualFontSize:NULL lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];    
        
        // Response Finish
        
        NSDate *responseFinDate  = [NSDate dateWithTimeIntervalSince1970:[risk getResponseFinish]];
        
        NSString *responseFinish = [NSString stringWithFormat:@"Finish: %@", [dateFormatter stringFromDate: responseFinDate]];
        
        size = [responseFinish sizeWithFont:mainFont minFontSize:FONT_SIZE actualFontSize:&actualFontSize forWidth:TOTAL_WIDTH lineBreakMode:UILineBreakModeTailTruncation];  
        
        CGFloat respFinish = (TOTAL_WIDTH - size.width) - 10;
        
        point = CGPointMake(respFinish, UPPER_ROW_TOP + 15);
        
        int respWidth = TOTAL_WIDTH - MIDDLE_COLUMN_OFFSET;
        
        [responseFinish drawAtPoint:point forWidth:respWidth withFont:mainFont minFontSize:FONT_SIZE actualFontSize:NULL lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];    
        
        // Responsible ***************
        
        point = CGPointMake(boundsX + LEFT_COLUMN_OFFSET, UPPER_ROW_TOP + 25);
        
        NSString *responseLabel = [NSString stringWithFormat:@"Resp.: %@", [risk getResponse_Owner]];
        
        [responseLabel drawAtPoint:point forWidth:LEFT_COLUMN_WIDTH withFont:mainFont minFontSize:FONT_SIZE actualFontSize:NULL lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
        
        
        
        // Set the color for the secondary text items.
        
        [secondaryTextColor set];       
        
        
        point = CGPointMake(boundsX + LEFT_COLUMN_OFFSET, LOWER_ROW_TOP);
        
        [abbreviation1 drawAtPoint:point forWidth:LEFT_COLUMN_WIDTH withFont:secondaryFont minFontSize:MIN_FONT_SIZE actualFontSize:NULL lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
        
        point = CGPointMake(boundsX + LEFT_COLUMN_OFFSET, LOWER_ROW_TOP2);
        
        [abbreviation2 drawAtPoint:point forWidth:LEFT_COLUMN_WIDTH withFont:secondaryFont minFontSize:MIN_FONT_SIZE actualFontSize:NULL lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
        
        
        //IMAGE ******************
        
        UIImage *image = [Risk setImage:[risk getImpact]];
        
        CGFloat imageY = (contentRect.size.height - image.size.height) - 10;// / 2;
        
        int imageStart = MIDDLE_COLUMN_OFFSET;
        
        point = CGPointMake(boundsX + (imageStart + 13), imageY - 10);
        
        [image drawAtPoint:point];
        
        
        UIImage *image2 = [Risk setImage:[risk getProbability]];
        
        CGFloat image2Y = (contentRect.size.height - image2.size.height) - 10;// / 2;
        
        
        int image2Start = 320.0 - image2.size.width;
        
        point = CGPointMake(boundsX + (image2Start - 32), image2Y - 10);
        
        [image2 drawAtPoint:point];
        
        //PROBABILITY *************
        
        NSString *probability = [NSString stringWithFormat:@"Probability: %d", [risk getProbability]];
        
        size = [probability sizeWithFont:secondaryFont minFontSize:MIN_FONT_SIZE actualFontSize:&actualFontSize forWidth:TOTAL_WIDTH lineBreakMode:UILineBreakModeTailTruncation];       
        
        CGFloat probFinish = (TOTAL_WIDTH - size.width) - 10;
        CGFloat probFinishY = (95 - size.height) - 5;
        
        point = CGPointMake(probFinish, probFinishY);
        
        //point = CGPointMake(boundsX + (image2Start - 45), 65);
        
        [probability drawAtPoint:point forWidth:LEFT_COLUMN_WIDTH withFont:secondaryFont minFontSize:actualFontSize actualFontSize:&actualFontSize lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
        
        //IMPACT ******************
        
        NSString *impact = [NSString stringWithFormat:@"Impact: %d", [risk getImpact]];
        
        size = [impact sizeWithFont:secondaryFont minFontSize:MIN_FONT_SIZE actualFontSize:&actualFontSize forWidth:TOTAL_WIDTH lineBreakMode:UILineBreakModeTailTruncation];
        
        point = CGPointMake(boundsX + MIDDLE_COLUMN_OFFSET + 5, 75);
        
        [impact drawAtPoint:point forWidth:LEFT_COLUMN_WIDTH withFont:secondaryFont minFontSize:actualFontSize actualFontSize:&actualFontSize lineBreakMode:UILineBreakModeTailTruncation baselineAdjustment:UIBaselineAdjustmentAlignBaselines];
        
    }
    
}

- (void)dealloc {
    
    [risk release];   
    [dateFormatter release];    
    [abbreviation1 release];
    [abbreviation2 release];
    
    [super dealloc];
    
}

@end
